package com.hkc.huakang.hkcpaperless;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.hkc.huakang.hkcpaperless.bin.OrderDetail;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MyAdapter extends BaseExpandableListAdapter{
    private Context mContext;
    private List<OrderDetail>  list=new ArrayList<OrderDetail>() {
    };
    public MyAdapter(Context context){
        this.mContext = context;

    }
    public  void setListData(List<OrderDetail> list2){
        this.list.clear();
        this.list.addAll(list2);
    }
    /**
     * 取得分组数
     */
    @Override
    public int getGroupCount() {
        return list.size();
    }
    /**
     * 取得指定分组的子元素数
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return list.size()>0?list.get(groupPosition).getOrderDetails().size():0;
    }

    /**
     * 获取指定组的数据对象。
     * @param groupPosition
     * @return
     */
    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    /**
     * 获取指定子项的数据对象。
     * @param groupPosition 每组的id
     * @param childPosition 子项的id
     * @return
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        //我们这里返回一下每个item的名称，以便单击item时显示
        return list.get(groupPosition).getOrderDetails().get(childPosition).getDrug_name();
    }

    /**
     * 得指定分组的ID
     * @param groupPosition 组的ID
     * @return groupPosition
     */
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /**
     * 判断分组和子项的ID是否稳定。
     * @return
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder groupHolder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.group_item,null);
            groupHolder = new GroupHolder();
            groupHolder.groupTitle =  convertView.findViewById(R.id.tv_group_title);
            groupHolder.orderNo =  convertView.findViewById(R.id.order_no);
            convertView.setTag(groupHolder);
        }else {
            groupHolder = (GroupHolder) convertView.getTag();
        }
        // 如果group被展开
//        if (isExpanded){
//            groupHolder.groupImg.setBackgroundResource(R.mipmap.down);
//        }else {
//            groupHolder.groupImg.setBackgroundResource(R.mipmap.right);
//        }
        groupHolder.groupTitle.setText(list.get(groupPosition).getP_name());
        groupHolder.orderNo.setText(list.get(groupPosition).getOrder_no());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.child_item, null);
            childHolder = new ChildHolder();

            childHolder.childTitle =  convertView.findViewById(R.id.drug_name);
            childHolder.drugTotality =  convertView.findViewById(R.id.drug_totality);
            childHolder.drugSpec =  convertView.findViewById(R.id.drug_spec);
            childHolder.drugUnit =  convertView.findViewById(R.id.drug_unit);
            convertView.setTag(childHolder);
        }else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        //childHolder.childImg.setBackgroundResource(list.get(groupPosition).getOrderDetails().get(childPosition).getMarkerImgId());
        childHolder.drugTotality.setText(list.get(groupPosition).getOrderDetails().get(childPosition).getQuantity());
        childHolder.childTitle.setText(list.get(groupPosition).getOrderDetails().get(childPosition).getDrug_name());
        childHolder.drugSpec.setText(list.get(groupPosition).getOrderDetails().get(childPosition).getDrug_spec());
        childHolder.drugUnit.setText(list.get(groupPosition).getOrderDetails().get(childPosition).getOrder_unit());
        return convertView;
    }

    /**
     * 判断指定子项是否可以被选中。
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    private class GroupHolder
    {

        TextView groupTitle;
        TextView orderNo;

    }
    private class ChildHolder
    {

        TextView childTitle;
        TextView drugSpec;
        TextView drugTotality;
        TextView drugUnit;
    }

}